package com.newstart.springdatajpa.project.controller;

import com.newstart.springdatajpa.common.api.ApiResult;
import com.newstart.springdatajpa.project.model.vo.CrudVo;
import com.newstart.springdatajpa.project.model.dto.CrudDto;
import com.newstart.springdatajpa.project.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * 简单增删改查
     * {
     *     "crud": "update"
     * }
     */
    @RequestMapping(value = "crudDatebase", method = RequestMethod.POST)
    public ApiResult<CrudVo> crudDatebase(@RequestBody CrudDto crudDto) {
        return ApiResult.success(articleService.crudDatebase(crudDto));
    }

    /**
     * 分页并排序
     *  {
     * 	    "pageNo": 0,
     * 	    "flag": 2,
     *      "article": {
     *                    "content": "文章内容"
     *                }
     *  }
     */
    @RequestMapping(value = "jpaPage", method = RequestMethod.POST)
    public ApiResult<CrudVo> jpaPage(@RequestBody CrudDto crudDto) {
        return ApiResult.success(articleService.jpaPage(crudDto));
    }

    /**
     * 模糊查询
     * {
     *     "flag": 2
     *     "article": {
     *         "title": "文章标题"
     *     }
     * }
     */
    @RequestMapping(value = "selectLike", method = RequestMethod.POST)
    public ApiResult<CrudVo> selectLike(@RequestBody CrudDto crudDto) {
        return ApiResult.success(articleService.selectLike(crudDto));
    }

    /**
     * 实体类查询
     * {
     *     "flag": 1,
     *     "article": {
     *         "id":12,
     *         "title": "文章",
     *         "content": "文章内容"
     *               }
     * }
     */
    @RequestMapping(value = "jpqlselect", method = RequestMethod.POST)
    public ApiResult<CrudVo> jpqlselect(@RequestBody CrudDto crudDto) {
        return ApiResult.success(articleService.jpqlselect(crudDto));
    }

    /**
     * 原生sql查询
     * @param crudDto
     * @return
     */
    @RequestMapping(value = "sqlselect", method = RequestMethod.POST)
    public ApiResult<CrudVo> sqlselect(@RequestBody CrudDto crudDto) {
        return ApiResult.success(articleService.sqlselect(crudDto));
    }

    /**
     * 动态查询
     * @param crudDto
     * @return
     */
    @RequestMapping(value = "dynamiQuery", method = RequestMethod.POST)
    public ApiResult<CrudVo> dynamiQuery(@RequestBody CrudDto crudDto) {
        return ApiResult.success(articleService.dynamiQuery(crudDto));
    }


    /**
     * 多表查询
     * @param crudDto
     * @return
     */
    @RequestMapping(value = "manyTablesQuery", method = RequestMethod.POST)
    public ApiResult<CrudVo> manyTablesQuery(@RequestBody CrudDto crudDto) {
        return ApiResult.success(articleService.manyTablesQuery(crudDto));
    }


}
